package com.ad.pet.app.login.fragment;

import android.app.Activity;

import com.ad.pet.App;
import com.ad.pet.api.ApiService;
import com.ad.pet.app.RxDisposable;
import com.ad.pet.common.data.http.AppSubscriber;
import com.ad.pet.common.structure.mvp.AbsBasePresenter;
import com.ad.pet.models.remote.User;

import javax.inject.Inject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by SEELE on 2018/3/21.
 */

public class AccountLoginPresenter extends AbsBasePresenter implements AccountLoginContract.Presenter{

    private ApiService mApiService;
    private AccountLoginContract.View mView;

    private Activity mActivity;

    private RxDisposable mRxDisposable;

    @Inject
    public AccountLoginPresenter(ApiService apiService,
                                 Activity activity, RxDisposable rxDisposable) {
        mApiService = apiService;
        mActivity = activity;
        mRxDisposable = rxDisposable;
    }

    @Override
    public void onCreate(AccountLoginContract.View view) {
        mView = view;
    }


    @Override
    public void onDestroy() {
        if (mRxDisposable != null)
            mRxDisposable.clear();
        if (mActivity != null) mActivity = null;
        mView = null;
    }




    @Override
    public void register(String phoneNumber,String smsCode,String password,String to_reference_codeBODY) {
        mRxDisposable.add(mApiService.phone_register(
                phoneNumber,
                smsCode,
                password,to_reference_codeBODY)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.phoneLoginSuccess(user);
                    }
                }));
    }

    @Override
    public void accountLogin(String account, String password ) {
        mRxDisposable.add(mApiService.phone_login(
                account,
                password, JPushInterface.getRegistrationID(App.getApp()).toString()  )
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.accountLoginSuccess(user);
                    }
                }));
    }

    @Override
    public void resetPassword(String pwd, String new_pwd, String new_pwd_reset) {
        mRxDisposable.add(mApiService.reset_pwd(pwd,
                new_pwd,
                new_pwd_reset)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.resetPwdSuccesss();
                    }
                }));
    }


    @Override
    public void getEmailCode(String phoneNumber,String scene, String second,String type) {
        mRxDisposable.add(mApiService.get_sms_code(  phoneNumber, scene,  second, type)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.getEmailCodeSuccess();
                    }
                }));
    }

    @Override
    public void forgetPwdReset(String phone, String code, String pwd) {
        mRxDisposable.add(mApiService.forget_pwd(phone,code,pwd)
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.forgetPwdResetSuccess();
                    }
                }));
    }

    @Override
    public void registJpush() {
        mRxDisposable.add(mApiService.reggistJpush()
                .subscribe(new AppSubscriber<User>(mView) {
                    @Override
                    public void next(User user) {
                        mView.registJpushSuccess();
                    }
                }));
    }


}
